package com.pet.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.common.constants.user.UserConstants.AdminConstans;
import com.pet.model.article.pojos.ApAuthor;
import com.pet.model.common.dtos.PageResponseResult;
import com.pet.model.common.dtos.ResponseResult;
import com.pet.model.common.enums.AppHttpCodeEnum;
import com.pet.model.media.dtos.WmUserDto;
import com.pet.model.media.pojos.WmUser;
import com.pet.model.user.dtos.AuthDto;
import com.pet.model.user.pojos.ApUser;
import com.pet.model.user.pojos.ApUserRealname;
import com.pet.user.feign.ArticleFeign;
import com.pet.user.feign.WemediaFeign;
import com.pet.user.mapper.ApUserMapper;
import com.pet.user.mapper.ApUserRealnameMapper;
import com.pet.user.mapper.WmUserMapper;
import com.pet.user.service.ApUserRealnameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private WmUserMapper wmUserMapper;

    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        //参数为空
        if (dto == null) {
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        dto.checkParam();
        WmUserDto wmUserDto = new WmUserDto();
        wmUserDto.setPage(dto.getPage());
        wmUserDto.setSize(dto.getSize());
        wmUserDto.setStatus(dto.getStatus());
        PageResponseResult page = wemediaFeign.page(wmUserDto);
        return page;
    }

    @Override
    @Transactional
//    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        //检查参数
        if (dto == null || dto.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //修改状态
        WmUserDto wmUserDto = new WmUserDto();
        wmUserDto.setId(dto.getId());
        wmUserDto.setStatus(dto.getStatus());
        wemediaFeign.update(wmUserDto);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 创建自媒体账号， 以及作者账号
     * @param dto
     * @return
     */
    private ResponseResult createWmUserAndAuthor(AuthDto dto) {
        //添加自媒体账号, 查询ap_user信息封装到wmuser中
        ApUserRealname aur = getById(dto.getId());
        ApUser apUser =  apUserMapper.selectById(aur.getUserId());
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 检测自媒体用户是否存在
        WmUser wmUser = wemediaFeign.findByName(apUser.getName());
        if (wmUser == null || wmUser.getId()==null) {
            wmUser = new WmUser();
            //设置ApUserId
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(1);
            wmUser.setPhone(apUser.getPhone());
            wemediaFeign.save(wmUser);
        }

        //创建作者账号
        createAuthor(wmUser);
        //修改ap_user标记
        apUser.setFlag((short)1);
        apUserMapper.updateById(apUser);
        return null;
    }

    /**
     * 创建自媒体账号
     * @param wmUser
     * @return
     */
    private void createAuthor(WmUser wmUser) {
        Integer apUserId = wmUser.getApUserId();
        ApAuthor apAuthor =  articleFeign.findByUserId(apUserId);
        if (apAuthor == null) {
            apAuthor = new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(AdminConstans.AUTH_TYPE);
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(apUserId);
            articleFeign.save(apAuthor);
        }
    }

    /**
     * 状态监测
     * @param status
     * @return
     */
    private boolean statusCheck(Short status) {
        if (status == null
                || ( !status.equals(AdminConstans.FAIL_AUTH)
                && !status.equals(AdminConstans.PASS_AUTH))) {
            return true;
        }
        return false;
    }
}